<template>
  <div class="main">
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/logo.png" />
      </div>
      <div class="user_info">
        <el-form
          label-position="right"
          label-width="auto"
          :rules="rules"
          ref="ruleForm"
          :model="form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="form.password" prefix-icon="el-icon-lock">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loginBtn">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VueShopHelloWorld',

  data() {
    return {
      form: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入账户名称', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入账户密码', trigger: 'blur' },
        ],
      },
    }
  },

  mounted() {},

  methods: {
    // 登录
    loginBtn() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const { data, meta } = await this.$axios.login(this.form)
          // 登录失败
          if (meta.status !== 200) return this.$message.error(meta.msg)
          // 登录成功,在会话存储里面保存token,跳转页面
          window.sessionStorage.setItem('token', data?.token)
          this.$router.push('/home')
        } else {
          return false
        }
      })
    },
    // 重置
    resetForm() {
      this.form = {
        username: 'admin',
        password: '123456',
      }
      this.$refs.ruleForm.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.main {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #eeee;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      height: 100px;
      width: 100px;
      position: absolute;
      top: -25%;
      left: 50%;
      transform: translate(-50%, 20%);
      overflow: hidden;
      border-radius: 50%;
      background-color: #fff;
      border: 1px solid #ccc;
      padding: 10px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid #ccc;
        background-color: #ccc;
      }
    }
    .user_info {
      margin-top: 90px;
      padding: 20px;
    }
  }
}
</style>
